/*  --------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen -----------------------------
    File Name      : Interrupt.c
    Author         : Fortiortech  Appliction Team
    Version        : V1.0
    Date           : 2020-04-10
    Description    : This file contains .C file function used for Motor Control.
    ----------------------------------------------------------------------------------------------------
                                       All Rights Reserved
    ------------------------------------------------------------------------------------------------- */
#include "MyProject.h"

void motoronoff(void);

extern uint8 data g_1mTick;                   ///< 1ms滴答信号，每隔1ms在SYSTICK定时器被置1，需在大循环使用处清零

//uint16 xdata spidebug[4] = {0};
/*  -------------------------------------------------------------------------------------------------
    Function Name  : LVW_TSD_INT
    Description    : LVW interrupt，芯片低压预警中断
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void LVW_TSD_INT(void) interrupt 0 // LVW & TSD interrupt
{
    if (ReadBit(LVSR, LVWIF))
    {
        if (ReadBit(LVSR, LVWF))
        {
            ClrBit(LVSR, LVWF);
        }
        
        ClrBit(LVSR, LVWIF);
    }
    
    if (TSDIF)
    {
        mcState = mcFault;    
        mcFaultSource = FaultTSD;     //TSD过温故障
        FaultProcess();               //错误处理函数
		    TSDIF = 0;
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : PO0_INT
    Description    : PO0_INT外部中断0，
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void PO0_INT(void) interrupt 1 /* LIN同步信号检测interrupt 1外部中断0，用于检测LIN的同步信号检测*/
{
    uint16 time = 0;
    
    if (IF0)
    {
        if (GP01 == 0) /*接收到下降沿，所以说上升沿为1？*/
        {
            if (Lin.RUN_flag == 0)
            {
                Lin.RUN_flag = 1;
                ClrBit(TIM3_CR1, T3EN); /*先停止计数*/
                TIM3__CNTR = 1;         /*清除time3的计数器*/
                Lin.Timeblank = 0;
                SetBit(TIM3_CR1, T3EN); /*使能计数*/
            }
        }
        else
        {
            if (Lin.RUN_flag == 1)
            {
                /*低电平持续时间满足13位的同步间隔断要求*/
                ClrBit(TIM3_CR1, T3EN); /*先停止计数*/
                Lin.Timeblank = TIM3__CNTR;//这个就是间隔时间吗？
                /*19200对应的同步时间 13位  0.677MS  2031计数值  同步阶段误差5%可以接收*/
                if ((Lin.Timeblank < 2230) && (Lin.Timeblank > 1930))
                {
                    Lin.RUN_flag = 11;
                    MODE_UART(); /*切换到串口模式*/
                }
                else
                {
                    Lin.RUN_flag = 0;
                }
                
                /*数据超时错误使用防止无法出串口中断 */
                /*数一组数据最大不会停留7ms 根据发送调度表计算*/
                TIM3__CNTR = 1;         /*清除time3的计数器*/
                SetBit(TIM3_CR1, T3EN); /*使能计数*/
            }
        }
        
        IF0 = 0; /* clear P01 interrupt flag*/
    }
}
/*  -------------------------------------------------------------------------------------------------
    Function Name  : EXTERN_INT
    Description    : 睡眠唤醒
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void EXTERN_INT(void) interrupt 2//这里是外部中断1，不做操作？
{
}

void DRV_ISR(void) interrupt 3
{
    static uint8 Section = 0;
    
    if (ReadBit(DRV_SR, DCIF)) /* 比较中断*/
    {          
		 			
        /**************计换相次数 记录位置移动圈数************************/
        if ((mcState == mcRun) || (mcState == mcStart))
        { 
					  BL_S_Function(); /*启动函数*/
						if((Section != TIM1_CR4)&&((TIM1_CR4 == 1)||(TIM1_CR4 == 4)))
						{
							GP12=~GP12;
						}
						Section = TIM1_CR4;
        }
				

				Duty_Function();      /* Duty调整函数   速度闭环计算*/
				
//         #if defined (SPI_DBG_SW)            //软件调试模式
//        {
//            spidebug[0] = TIM1__RCNTR;
//            spidebug[1] = 0;
//            spidebug[2] = 0;
//            spidebug[3] = 0;
//        }
//        #endif

				DRV_SR = (DRV_SR | SYSTIF) & ~(DCIF);                            // 清零DCIF中断标志位 

    }
}

/* TIM1中断 */
void TIM1_INT(void) interrupt 5
{
    if (ReadBit(TIM1_SR, T1PDIF)) //位置检测中断标志位
    {
        ClrBit(TIM1_SR, T1PDIF);
    }
}


#if(SPEED_MODE!= PWMMODE)
/*---------------------------------------------------------------------------*/
/*  Name     :   void TIM3_INT(void) interrupt 9
    /* Input    :   NO
    /* Output   :   NO
    /* Description: LIN总线计时使用
/*---------------------------------------------------------------------------*/
void TIM3_INT(void) interrupt 9
{
    if (ReadBit(TIM3_CR1, T3IP)) /*比较中断事件*/
    {
        ClrBit(TIM3_CR1, T3IP);
    }
    
    if (ReadBit(TIM3_CR1, T3IR)) /*比较中断事件*/
    {
        ClrBit(TIM3_CR1, T3IR);
    }
    
    if (ReadBit(TIM3_CR1, T3IF)) /*溢出中断事件*/
    {
        if (Lin.RUN_flag == 1)
        {
            Lin.RUN_flag = 0; /*同步间隔段超时错误*/
            Lin.Err = ERR_BLANK;
        }
        else if (Lin.RUN_flag == 11)
        {
            Lin.RUN_flag = 0; /*同步信号超时错误*/
        }
        else if (Lin.RUN_flag == 12)
        {
            Lin.RUN_flag = 0; /*接收ID信号超时错误*/
            Lin.Err = ERR_PID;
        }
        else if (Lin.RUN_flag == 13)
        {
            Lin.RUN_flag = 0; /*接收数据超时错误*/
            Lin.Err = ERR_DATA;
        }
        else if (Lin.RUN_flag == 23)
        {
            Lin.RUN_flag = 0; /*发送数据超时错误*/
        }
        
        Cutover_IO; /*切换回io模式*/
        /* put_char(0X55);//测试串口*/
        ClrBit(TIM3_CR1, T3IF);
    }
}
#endif

#if(SPEED_MODE== PWMMODE)
/**
 * @brief        Capture PWM ，用于PWM调速
 * @date         2022-08-07
 */
void TIM3_INT(void) interrupt 9
{
	if (ReadBit(TIM3_CR1, T3IR))
	{
		ClrBit(TIM3_CR1, T3IR);
	}

	if (ReadBit(TIM3_CR1, T3IP))//周期中断
	{
		if (mcPwmInput.PwmFlag != 1) //若正在计算占空比则不更新
		{
			mcPwmInput.PwmCompare = TIM3__DR;
			mcPwmInput.PwmArr = TIM3__ARR;
			mcPwmInput.PwmUpdateFlag = 1;
		}

		ClrBit(TIM3_CR1, T3IP);
	}

	if (ReadBit(TIM3_CR1, T3IF))
	{
		if (ReadBit(P1, PIN1))//PWM 100%输出
		{
			mcPwmInput.PwmCompare = 4000;
			mcPwmInput.PwmArr = 4000;
		}
		else//PWM 为0%
		{
			mcPwmInput.PwmCompare = 0;
			mcPwmInput.PwmArr = 4000;
		}

		mcPwmInput.PwmUpdateFlag = 1;
		ClrBit(TIM3_CR1, T3IF);
	}

}
#endif

/*  -------------------------------------------------------------------------------------------------
    Function Name  : SYStick_INT
    Description    : 1ms定时器中断（SYS TICK中断），用于处理附加功能，如控制环路响应、各种保护等。中断优先级低于FO中断和FOC中断。
    Date           : 2020-04-10
    Parameter      : None
    ---------------------------  ---------------------------------------------------------------------- */

uint16 test_DCbus;
void SYStick_INT(void) interrupt 10
{
    // SYS TICK中断
    if (ReadBit(DRV_SR, SYSTIF))
    {
        SetBit(ADC_CR, ADCBSY);
        g_1mTick = 1;
        
		DRV_SR = (DRV_SR | DCIF) & ~(SYSTIF);           // 清零标志位
		
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : TIM4_INT
    Description    : TIM4主要用于150度方波的插入
    Date           : 2021-03-30
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void TIM4_INT(void) interrupt 11
{
    uint8 TIM1_CR4_150;
    
    if (ReadBit(TIM4_CR1, T4IF))
    {      
        ClrBit(TIM4_CR1, T4EN); //关闭TIM4
        ClrBit(TIM4_CR1, T4IF); //清除上溢中断标志位
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : CMP3_INT
    Description    : CMP3：硬件比较器过流保护，关断输出，中断优先级最高
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void CMP3_INT(void) interrupt 12
{
    if (ReadBit(CMP_SR, CMP3IF))
    {
        mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
        FaultProcess();
        mcState = mcFault;
        ClrBit(CMP_SR, CMP3IF);
    }
}

/* -------------------------------------------------------------------------------------------------
    Function Name  : LIN_IRQ
    Description    : LIN中断函数
    Date           : 2020-07-20
    Parameter      : None
------------------------------------------------------------------------------------------------- */
void LIN_IRQ(void) interrupt 14
{
   
}
